package my.pack.tm;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayImg extends Activity{
	ImageView img;  
	TextView tv;
	Button back;
	int sequence [];
	static int counter=0;
	int lvl;
	Intent i;
    public void onCreate (Bundle savedInstanceState){
    	
    	  super.onCreate(savedInstanceState);
    	  setContentView(R.layout.images);
    	  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	  tv = (TextView)findViewById(R.id.info);
    	  back = new Button(this);
  		  back = (Button)findViewById(R.id.backB);
  	
    	img=(ImageView)findViewById(R.id.img);
    	i = getIntent();
    	lvl= Integer.parseInt(i.getStringExtra("level"));
    	sequence = new int [lvl];
    	
    	 final MyCount counter = new MyCount((lvl+1)*1500,1500); //counter for displaying the sequence of symbols, 1.5 s for each image
    	 counter.start();
    	 
         back.setOnClickListener(new View.OnClickListener() {  
   			
 			public void onClick(View v) { 
 				counter.cancel();
 				Intent intent=getIntent();
 				setResult(1,intent);         //set the result code 1  
 				finish();    
 			}
 		});  
         
    }
    public void onActivityResult (int requestCode, int resultCode, Intent data){  
    	if(requestCode==3)                                                       
    			finish();        
    	
    	}
    
  //-----------------------------------------------  
  public class MyCount extends CountDownTimer{
  	
  	public MyCount(long millisInFuture, long countDownInterval) {
  	super(millisInFuture, countDownInterval);
  	}

  	@Override
  	public void onFinish() { //when the sequence is over go to the solve game activity
  		counter=0;
  		Toast.makeText( getApplicationContext(),"Done!", Toast.LENGTH_SHORT).show(); 
  		Intent intent=new Intent(DisplayImg.this, SolveGame.class);
		intent.putExtra("sequence", sequence);
		startActivityForResult(intent, 3);  //start another activity, AddCity, with request code 3
  	}

  	@Override
  	public void onTick(long millisUntilFinished) { //on each tick of the counter change image and display the correct label
  		setImage(img);
  		tv.setText("Image "+counter);
  	}
  	
  	}
//-------------------------------------------------------
  
  public void setImage(ImageView img){ //set a random image each time the counter ticks
		int random=(int) (Math.random()*8);
		if(random==0) random++;
		sequence[counter++]=random; //remember the image in the sequence array
	switch(random){
	case 1:
		img.setImageResource(R.drawable.shape1);
		break;
	case 2:
		img.setImageResource(R.drawable.shape2);
		break;
	case 3:
		img.setImageResource(R.drawable.shape3);
		break;
	case 4:
		img.setImageResource(R.drawable.shape4);
		break;
	case 5:
		img.setImageResource(R.drawable.shape5);
		break;
	case 6:
		img.setImageResource(R.drawable.shape6);
		break;
	case 7:
		img.setImageResource(R.drawable.shape7);
		break;
	case 8:
		img.setImageResource(R.drawable.shape8);
		break;
	default:
		img.setImageResource(R.drawable.shape1);
		break;
		
	}
  }

}
